package com.glory.wxelectricitypda.activity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.bean.BarCodeCheckBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.base.FreeUI_BaseActivity;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class BarCodeCheckActivity extends FreeUI_BaseActivity{


    @BindView(R.id.llMain)
    LinearLayout llMain;
    @BindView(R.id.etBarCode)
    FreeUI_ClearEditText etBarCode;
    @BindView(R.id.etBoxTraceCode)
    FreeUI_ClearEditText etBoxTraceCode;
    @BindView(R.id.btGetBarCodeCheck)
    Button btGetBarCodeCheck;
    private BarCodeCheckBean mBarCodeCheckBean;
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_barcodecheck;
    }

    @Override
    protected void initView() {
        freeUI_titleView.setTitle("质检");
    }

    @Override
    protected void initData() {

        btGetBarCodeCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject map = new JSONObject();
                JSONObject jsonObject = new JSONObject();
                try {
                    map.put("ACTIONTYPE", "EUITPRINTCHECK");
                    map.put("BOXTRACECODE", etBoxTraceCode.getText().toString().trim());
                    map.put("BARCODE", etBarCode.getText().toString().trim());
                    jsonObject.put("Data",map);
                    jsonObject.put("MessageName","EUIT.EUITPRINTCHECK");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<BarCodeCheckBean>>(
                        new TypeToken<ResponseBean<BarCodeCheckBean>>() {
                        }.getType(), getSupportFragmentManager(), this) {
                    @Override
                    public void onSuccess(Response<ResponseBean<BarCodeCheckBean>> response) {
                        if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                            showToast("合格", true);
                            etBarCode.setText("");
                            etBoxTraceCode.setText("");
                            etBoxTraceCode.requestFocus();
                        }
                    }

                    @Override
                    public void onError(Response<ResponseBean<BarCodeCheckBean>> response) {
                        super.onError(response);
                        loadError(response.getException(), "EUIT.EUITPRINTCHECK");
                    }
                });
            }
        });

        etBarCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() < 0) {
                        FreeApi_ToastUtils.showFreeToast("扫描信息有误", BarCodeCheckActivity.this, false, 1);
                    }else {
                        FreeApi_LogUtils.saveErrorMessages(s.toString(), "扫描信息");
//                        String[] split = s.toString().split("|");
//                        etBarCode.setText("${"+split[0]+"}|${"+split[1]+"}|${"+split[2]+"}|${"+split[3]+"}");
                        etBarCode.setText(s.toString());
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

        etBoxTraceCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    if (s.length() > 0) {
                        etBarCode.requestFocus();
                    } else {
                        FreeApi_ToastUtils.showFreeToast("扫描信息有误", BarCodeCheckActivity.this, false, 1);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        return new FreeUI_AddViewEntity(getIntent().getStringExtra(FreeApi_StaticMembers.SHOW_PLUGIN_KEY), llMain);
    }

}
